<?php
namespace Intendant\Controller;
use Think\Controller;
use Think\Auth;
class IndexController extends Controller {
	public function _initialize(){
		if(!isset($_SESSION['admin_user']))
        {
            $this->redirect('Login/index');
        }
	}
    public function userPass(){
        if(IS_POST && I("editUser") && isset($_SESSION['admin_uid']))
        {
            $rules = array(
                // 验证确认密码是否和密码一致
                array('passworded','password',-7,0,'confirm'),
            );
            $user = M('user');
            if($user->validate($rules)->create()){
                $user->uid = $_SESSION['admin_uid'];
                if(I('password') == '' || I('passworded') == ''){//当没有修改密码时
                    unset($table_User->password);
                }
                else
                {
                    $user->password = jiami(I('password'));
                }
                if($status = $user->save())
                {
                    $this->ajaxReturn(-20);
                }
                else
                {
                    $this->ajaxReturn(-21);
                }

            }
            else
            {
                $this->ajaxReturn($user->getError());
            }
        }
        $this->display();
    }
    public function deleteCache(){
        if(IS_POST){
            $status = delDirAndFile('./Application/Runtime/',1);
            $msg = returnMsg("清空缓存成功","清空缓存失败",$status);
            $this->ajaxReturn($msg);
        }
    }
    public function index(){
    	// echo session('admin_user');
    	//初始化模板变量
        $where = array(
            "pid" => 0,
            "isshow" => 1
        );
    	$this->topNav = M("AuthRule")->where($where)->order("sort asc")->select();
    	$this->display();

    }
    //获取左侧菜单
    public function getLeftMenu(){
        $adminuid = session('admin_uid');
        if($adminuid == '1'){//区分是否管理员用户
            $table_AuthRule = M('AuthRule')->field('id')->select();
            foreach($table_AuthRule as $v){
                $AuthRules[] = $v['id'];
            }
        } else {
            $Auth = new Auth();
            $AuthGroups = $Auth->getGroups($adminuid);//获取用户所有组
            foreach($AuthGroups as $v){
                if($v['group_id'] == '1'){//区分是否该角色拥有超级管理员角色
                    $AuthRules = array();
                    $table_AuthRule = M('AuthRule')->field('id')->select();
                    foreach($table_AuthRule as $v){
                        $AuthRules[] = $v['id'];
                    }
                    break;//超级管理员中止循环，具有所有规则权限
                } else {
                    $AuthRules[] = $v['rules'];
                }
            }
        }
        //组合用户所有角色规则ID

        $AuthRules = implode(',', $AuthRules);
        // p($AuthRules);
        $where = array(
            'pid'=>I('pid','','int'),
            'isshow'=>1,
            'id'=>array('in',$AuthRules)
        );
        $table_AuthRule = M("AuthRule")->where($where)->order("sort asc")->field("id,title as text,icon")->select();
        foreach($table_AuthRule as $k3 => $v3)
        {
            $table_AuthRule[$k3]['iconCls'] = $v3['icon'];
        }
        foreach($table_AuthRule as $k => $v){
            $where = array(
                'pid'=>$v['id'],
                'isshow'=>1,
                'id'=>array('in',$AuthRules)
            );
            $table_AuthRule_son = M("AuthRule")->where($where)->order("sort asc")->field("id,title as text,icon,name as url")->select();
            foreach($table_AuthRule_son as $k2 => $v2){
                $table_AuthRule_son[$k2]['url'] = __MODULE__."/".$v2['url'];
                $table_AuthRule_son[$k2]['type'] = true;
                $table_AuthRule_son[$k2]['iconCls'] = $v2['icon'];
            }
            $table_AuthRule[$k]['children'] = $table_AuthRule_son;
        }

        $this->ajaxReturn($table_AuthRule);
        // dump($table_AuthRule);

    }
    public function home()
    {
    	//服务器信息
        $info = array(
            '操作系统' => PHP_OS,
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name(),
            'MYSQL版本' => mysql_get_server_info(),
            '产品名称' => '<font color="#FF0000">' . C("HEMA_APPNAME") . '</font>' . "&nbsp;&nbsp;&nbsp; [<a href='http://www.xfnmc.com' target='_blank'>访问官网</a>]",
            '用户类型' => '<font color="#FF0000" id="server_license">开源使用</font>',
            '产品版本' => '<font color="#FF0000">' . C("HEMA_VERSION") . '</font>，最新版本：<font id="server_version">'.C("HEMA_VERSION").'</font>',
            '产品流水号' => '<font color="#FF0000">' . C("HEMA_BUILD"). '</font>，最新流水号：<font id="server_build">'.C("HEMA_BUILD").'</font>',
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . "秒",
            '剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
        );

        $this->assign('server_info', $info);
    	$this->display();
    }
    public function logout(){
        $login = new \Intendant\Model\LoginModel();
        $login->logout();
        $this->redirect('Login/index');
    }
}